<?php
class TopListController  extends Controller {

	public function __construct($data) {
        parent::__construct($data);
    }
    
    public function index() {
    	 require_once( '../system/ecjp_m.php' );
    	  $this->_load_php_file("chklogin.php");
         if(!isset($_SESSION['USER'])){	    	
	    require_once( '../system/cookie.php' );  
	    }
	      $this->_load_php_file("Class_FUNC.php");
	    
    	  $this->_load_mdao_class("t_m_goods");
	      $goods_dao = new t_m_goods;
    	 
	       if ( isset($_SESSION['USER']) ){
	       	   $user = $_SESSION['USER'];
	       	   $login_id = $user['USER_id'];
	       	   $db = new Class_M_DAO;
    		   $sql = sprintf("SELECT * FROM D_HOPE WHERE login_id = '%s' AND del<>'*' ",
			   mysql_real_escape_string( $login_id ));
    		   $data = $db->get_rows( $sql );
	       	   
	       	    if (isset($user))
	            $this->assign('user', $user);
	       }
	   
	      	      	      
    	 //売上ランキング
	     $sql = "SELECT * FROM `d_goods` WHERE  goods_mark1='1' order by sellbegin desc LIMIT 0,10";
	     $charts_data = $goods_dao->get_rows($sql);
	     foreach ($charts_data as &$value){
	          if(isset($value['goods_image']) && !empty($value['goods_image'])){
	     	     $value['goods_image'] = Class_FUNC::get_imagepath($value['id'],$value['goods_image'],1);
	       }
	       if(isset($value['goods_unitprice']) && !empty($value['goods_unitprice'])){
	          	 $value['goods_rate'] = $value['goods_unitprice']+$value['goods_unitprice'] * RATE;
	          }
	     }     
	     if (isset($charts_data))
	     $this->assign('charts_data',$charts_data);
	     
	     //カテゴリで商品
       //  $wherearr = array();
       //  $wherearr[] = sprintf("(a.goods_mark1= '%s' and a.goods_status= '%s'  )",'1','1');
	   //  $goods_data = $goods_dao->search_csv($wherearr);
	   //  if (isset($goods_data))
	   //  $this->assign('goods_data', $goods_data);
	     
	    $forms = $this->g; 
        $wherearr = array();
        if (isset($forms['category_id']) && (!empty($forms['category_id']) )) {
        	$keyword = $forms['category_id'];
        	$this->assign('category_id', $forms['category_id']);
            $wherearr[] = sprintf("(a.category_id = '%s'  )", $keyword);
            
            
              $this->_load_php_file("Class_FUNC.php");
	          $goodsset = Class_FUNC::get_goodsset();
	          $set = $goodsset[$forms['category_id']];
             
        }
        
      
	     
	   if (isset($forms['page']) && is_numeric($forms['page'])) {
            $dosearch = TRUE;
            $page['current'] = $forms['page'];
        } else {
            $page['current'] = 1;
        }
       

       if(isset($set)){
        	if(isset($set['listnumber']) && !empty($set['listnumber'])){
        		 $page['item'] = $set['listnumber'];
        	}else {
        		 $page['item'] =  PAGE_MAX;        		 
        	}
        	 $this->assign('set', $set);
        }else {
        	 $page['item'] =  PAGE_MAX;
        }
        
        
        $goods_data = $goods_dao->search($wherearr, $page);
	     
 
        if(isset($data) && !empty($data)){
        	   foreach ($data as $value) {
                 if(isset($value['goods_id']) && (!empty($value['goods_id']))){
    				 $id[] = $value['goods_id']; 				 
    			}           
                  
              }
                
           foreach( $goods_data as $kcol => $chkval ) {          	   
			 if(in_array($goods_data[$kcol]['id'],$id)){
			 	$goods_data[$kcol]['Y'] = 'Y';
			 }
			 
           }
        }
        
         foreach ($goods_data as &$value){
	          if(isset($value['goods_image']) && !empty($value['goods_image'])){
	     	     $value['goods_image'] = Class_FUNC::get_imagepath($value['id'],$value['goods_image'],1);
	       }
         if(isset($value['goods_unitprice']) && !empty($value['goods_unitprice'])){
	          	 $value['goods_rate'] = $value['goods_unitprice']+$value['goods_unitprice'] * RATE;
	          }
	     }
        
        
        
        if(isset($forms))
        $this->assign('forms', $forms);
        
        if (isset($goods_data))
	    $this->assign('goods_data', $goods_data);
	     
	    if (isset($page))
        $this->assign('page', $page);
	     
	     $this->assign('pageing', $this->fetch('pc/toppage.html'));
	     $this->assign('chartsing', $this->fetch('pc/charts.html'));
	     
	     $this->assign('pc_head', $this->fetch('pc/pc_head.html'));
	     $this->assign('pc_foot', $this->fetch('pc/pc_foot.html'));
    	 $this->display('pc/list.html');
    }
    
    public  function add(){
    	     require_once( '../system/ecjp_m.php' );
     	         $this->_load_php_file("Class_DB.php");
     	         $p=$this->p;
     	         
     	         $good_id = $p['goods_id'];
     	         $login_id = $p['login_id'];
     	         $sql = sprintf("INSERT INTO D_HOPE(login_id,goods_id) VALUES ('%s','%s') ",
			     mysql_real_escape_string( $login_id ),
			     mysql_real_escape_string( $good_id ));
     	         
     	         $db = new Class_DB;
     	         $db->query( $sql );   
    	
    }
}

?>